package win.smartown.app.gank.type.data;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import win.smartown.app.R;
import win.smartown.app.base.BaseActivity;
import win.smartown.app.base.SuperActivity;
import win.smartown.app.gank.entity.Data;
import win.smartown.easyAdapter.ColorDivider;
import win.smartown.easyAdapter.EasyAdapter;
import win.smartown.easyAdapter.EasyViewHolder;

/**
 * Created by smartown on 2017/5/5 10:24.
 * Desc:
 */
public class TypeDataActivity extends BaseActivity implements Contract.View {

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    private TypeDataPresenter presenter;
    private RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseView(R.layout.activity_type_data);
        init();

        swipeRefreshLayout = findView(R.id.swipe_refresh);
        recyclerView = findView(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new ColorDivider(Color.parseColor("#cccccc"), 1));
        recyclerView.setAdapter(adapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.refresh();
            }
        });

        presenter.refresh();
    }

    private void init() {
        presenter = new TypeDataPresenter(this, getIntent().getStringExtra("title"));
        adapter = new EasyAdapter(this) {

            @Override
            public int getItemLayout(int viewType) {
                return R.layout.item_type;
            }

            @Override
            public void onBindViewHolder(EasyViewHolder holder, int position) {
                final Data data = presenter.getDataList().get(position);
                holder.getTextView(R.id.text).setText(data.getDesc());
            }

            @Override
            public int getItemCount() {
                return presenter.getDataList().size();
            }
        };
    }

    @Override
    public SuperActivity getSuperActivity() {
        return this;
    }

    @Override
    public void startLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void finishLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onDataChanged() {
        adapter.notifyDataSetChanged();
    }

}
